package com.xiaol.ray.atomikosdemo.util;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * User: Ray.Shaw <p />
 * Time: 2019/10/28 14:41  <p />
 * Version: V1.0  <p />
 * Description:  <p />
 */
public class StringUtils {
    /**
     * 判断对象是否Empty(null或元素为0)
     * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
     *
     * @param obj
     *         待检查对象
     * @return boolean 返回的布尔值
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null || "".equals(obj)) {
            return true;
        }

        if (obj instanceof String) {
            return ((String) obj).trim().length() == 0;
        } else if (obj instanceof Collection) {
            return ((Collection) obj).isEmpty();
        } else if (obj instanceof Map) {
            return ((Map) obj).size() == 0;
        } else if (obj instanceof List) {
            return ((List) obj).size() == 0;
        }

        return false;
    }

    /**
     * 判断对象是否为NotEmpty(!null或元素大于0)
     * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
     *
     * @param obj
     *         待检查对象
     * @return boolean 返回的布尔值
     */
    public static boolean isNotEmpty(Object obj) {
        if (obj == null || "".equals(obj)) {
            return false;
        }
        if (obj instanceof String) {
            return ((String) obj).trim().length() != 0;
        } else if (obj instanceof Collection) {
            return !((Collection) obj).isEmpty();
        } else if (obj instanceof Map) {
            return ((Map) obj).size() != 0;
        } else if (obj instanceof List) {
            return ((List) obj).size() != 0;
        }

        return true;
    }
}
